/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.client.decorator.impl;

import org.soupframework.gwt.designerpanel.client.decorator.ObjectDecorator;

import com.google.gwt.dom.client.Style.BorderStyle;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;

public class ResizePointsWidgetDecorator<T> implements ObjectDecorator<T> {

	private BorderStyle borderStyle = BorderStyle.DASHED;
	private int borderWidth = 1;
	private String borderColor = "#0000ff";

	public ResizePointsWidgetDecorator() {
	}

	public ResizePointsWidgetDecorator(BorderStyle borderStyle, int borderWidth, String borderColor) {
		super();
		this.borderStyle = borderStyle;
		this.borderWidth = borderWidth;
		this.borderColor = borderColor;
	}

	@Override
	public void decorateObject(T object, Widget objectView) {
		if(objectView != null && objectView.getParent() != null && objectView.getParent().getElement() != null){
			int left = DOM.getIntStyleAttribute(objectView.getElement(), "left");
			int top = DOM.getIntStyleAttribute(objectView.getElement(), "top");
			int width = objectView.getOffsetWidth();
			int height = objectView.getOffsetHeight();
			int right = left + width;
			int bottom = top + height;
			int middleX = (left + right) / 2;
			int middleY = (top + bottom) / 2;

			Element pe = objectView.getParent().getElement();

			//Element nSelDiv = createSelectionDecoratorLine(w.getElement().getId() + "-n-sel", "borderTop", left - 1, top - 1, width + 3, 1);
			//Element sSelDiv = createSelectionDecoratorLine(w.getElement().getId() + "-s-sel", "borderBottom", left - 1, bottom + 2, width + 3, 1);
			//Element eSelDiv = createSelectionDecoratorLine(w.getElement().getId() + "-e-sel", "borderRight", right + 2, top - 1, 1, height + 3);
			//Element wSelDiv = createSelectionDecoratorLine(w.getElement().getId() + "-w-sel", "borderLeft", left - 1, top - 1, 1, height + 3);
			Element selDiv = createSelectionDecorator(objectView.getElement().getId() + "-sel", left - 1, top - 1, width + 3, height + 3);

			Element sDiv = createResizePoint(objectView.getElement().getId() + "-s-drag-point", middleX - 2, bottom + 3);
			Element swDiv = createResizePoint(objectView.getElement().getId() + "-sw-drag-point", left - 6, bottom + 3);
			Element wDiv = createResizePoint(objectView.getElement().getId() + "-w-drag-point", left - 6, middleY - 2);
			Element nwDiv = createResizePoint(objectView.getElement().getId() + "-nw-drag-point", left - 6, top - 6);
			Element nDiv = createResizePoint(objectView.getElement().getId() + "-n-drag-point", middleX - 2, top - 6);
			Element neDiv = createResizePoint(objectView.getElement().getId() + "-ne-drag-point", right + 3, top - 6);
			Element eDiv = createResizePoint(objectView.getElement().getId() + "-e-drag-point", right + 3, middleY - 2);
			Element seDiv = createResizePoint(objectView.getElement().getId() + "-se-drag-point", right + 3, bottom + 3);

			//pe.appendChild(nSelDiv);
			//pe.appendChild(sSelDiv);
			//pe.appendChild(eSelDiv);
			//pe.appendChild(wSelDiv);
			pe.appendChild(selDiv);

			pe.appendChild(sDiv);
			pe.appendChild(swDiv);
			pe.appendChild(wDiv);
			pe.appendChild(nwDiv);
			pe.appendChild(nDiv);
			pe.appendChild(neDiv);
			pe.appendChild(eDiv);
			pe.appendChild(seDiv);
		}
	}

	@Override
	public void undecorateObject(T object, Widget objectView) {
		if(objectView != null && objectView.getParent() != null && objectView.getParent().getElement() != null){
			Element pe = objectView.getParent().getElement();

			/*Element nSelDiv = DOM.getElementById(pe.getId() + "-n-sel");
			if(nSelDiv != null)	DOM.removeChild(pe, nSelDiv);
			Element wSelDiv = DOM.getElementById(pe.getId() + "-w-sel");
			if(wSelDiv != null)	DOM.removeChild(pe, wSelDiv);
			Element eSelDiv = DOM.getElementById(pe.getId() + "-e-sel");
			if(eSelDiv != null)	DOM.removeChild(pe, eSelDiv);
			Element sSelDiv = DOM.getElementById(pe.getId() + "-s-sel");
			if(sSelDiv != null)	DOM.removeChild(pe, sSelDiv);*/
			Element selDiv = DOM.getElementById(pe.getId() + "-sel");
			if(selDiv != null)	DOM.removeChild(pe, selDiv);

			Element sDiv = DOM.getElementById(pe.getId() + "-s-drag-point");
			if(sDiv != null)	DOM.removeChild(pe, sDiv);
			Element swDiv = DOM.getElementById(pe.getId() + "-sw-drag-point");
			if(swDiv != null)	DOM.removeChild(pe, swDiv);
			Element wDiv = DOM.getElementById(pe.getId() + "-w-drag-point");
			if(wDiv != null)	DOM.removeChild(pe, wDiv);
			Element nwDiv = DOM.getElementById(pe.getId() + "-nw-drag-point");
			if(nwDiv != null)	DOM.removeChild(pe, nwDiv);
			Element nDiv = DOM.getElementById(pe.getId() + "-n-drag-point");
			if(nDiv != null)	DOM.removeChild(pe, nDiv);
			Element neDiv = DOM.getElementById(pe.getId() + "-ne-drag-point");
			if(neDiv != null)	DOM.removeChild(pe, neDiv);
			Element eDiv = DOM.getElementById(pe.getId() + "-e-drag-point");
			if(eDiv != null)	DOM.removeChild(pe, eDiv);
			Element seDiv = DOM.getElementById(pe.getId() + "-se-drag-point");
			if(seDiv != null)	DOM.removeChild(pe, seDiv);
		}
	}

	@SuppressWarnings("unused")
	private Element createSelectionDecoratorLine(String name, String border, int left, int top, int width, int height) {
		Element div = DOM.getElementById(name);
		if(div == null){
			div = DOM.createDiv();
			div.setId(name);
			div.getStyle().setPosition(Position.ABSOLUTE);
			div.getStyle().setLeft(left, Unit.PX);
			div.getStyle().setTop(top, Unit.PX);
			div.getStyle().setWidth(width, Unit.PX);
			div.getStyle().setHeight(height, Unit.PX);
			div.getStyle().setZIndex(997);
			DOM.setStyleAttribute(div, border, this.borderWidth + "px " + this.borderStyle.toString() + " " + this.borderColor);
		}
		return div;
	}

	private Element createSelectionDecorator(String name, int left, int top, int width, int height) {
		Element div = DOM.getElementById(name);
		if(div == null){
			div = DOM.createDiv();
			div.setId(name);
			div.getStyle().setPosition(Position.ABSOLUTE);
			div.getStyle().setLeft(left, Unit.PX);
			div.getStyle().setTop(top, Unit.PX);
			div.getStyle().setWidth(width, Unit.PX);
			div.getStyle().setHeight(height, Unit.PX);
			div.getStyle().setZIndex(997);
			div.getStyle().setBorderStyle(this.borderStyle);
			div.getStyle().setBorderWidth(this.borderWidth, Unit.PX);
			div.getStyle().setBorderColor(this.borderColor);
		}
		return div;
	}

	private Element createResizePoint(String name, int left, int top) {
		Element div = DOM.getElementById(name);
		if(div == null){
			div = DOM.createDiv();
			div.setId(name);
			div.getStyle().setPosition(Position.ABSOLUTE);
			div.getStyle().setLeft(left, Unit.PX);
			div.getStyle().setTop(top, Unit.PX);
			div.getStyle().setWidth(4, Unit.PX);
			div.getStyle().setHeight(4, Unit.PX);
			div.getStyle().setZIndex(998);
			div.getStyle().setBackgroundColor("#EEEEEE");
			div.getStyle().setBorderStyle(BorderStyle.SOLID);
			div.getStyle().setBorderWidth(1, Unit.PX);
			div.getStyle().setBorderColor("#0000DD");
		}
		return div;
	}
}
